/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minesweeper;

/**
 *
 * @author Gustavo
 */
public class Maze {

    private int BOMBCODE = 9;
    private int[][] maze;
    private int rows, columns, bombs, totalToBeReaveled;

    /**
     * Generate a new maze with the number defined of rows, columns and bombs.
     * The default bomb code is BOMBCODE
     *
     * @param rows
     * @param columns
     * @param bombs
     * @return
     */
    public Maze(int rows, int columns, int bombs) throws Exception {
        if (bombs < columns * rows) {
            try {
                this.rows = rows;
                this.columns = columns;
                this.bombs = bombs;
                this.totalToBeReaveled = (rows * columns) - bombs;
                generate();
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception("Maze not generated.");
            }
        } else {
            throw new Exception("Number of bombs is too big");
        }
    }

    public int[][] generate() {

        int maze1[][] = new int[columns][rows];
        int contBombs = 0;
        while (contBombs < bombs) {
            int x = (int) (Math.random() * columns);
            int y = (int) (Math.random() * rows);
            if (maze1[x][y] != getBOMBCODE()) {
                maze1[x][y] = getBOMBCODE();
                contBombs++;
            }
        }

        detectBombs(maze1, columns, rows);
        this.maze = maze1;
        // print maze
      /*  System.out.print("\n");
        for (int[] i : maze) {
            System.out.print("\n");
            for (int x : i) {
                System.out.print(x + "|");
            }
        }*/
        return maze1;

    }

    private void detectBombs(int[][] maze, int columns, int rows) {
        //System.out.println(rows+" - "+columns);
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < columns; x++) {
                if (maze[x][y] == getBOMBCODE()) {

                    /*
                     * y o -origem | 0 0 0 | 0 9 0 y 0 0 0 x-------x
                     */
                    if (x > 0) {
                        if (maze[x - 1][y] != getBOMBCODE()) {
                            maze[x - 1][y]++;
                        }
                        if (y > 0) {
                            if (maze[x - 1][y - 1] != getBOMBCODE()) {
                                maze[x - 1][y - 1]++;
                            }
                        }
                        if (y < columns - 1) {
                            if (maze[x - 1][y + 1] != getBOMBCODE()) {
                                maze[x - 1][y + 1]++;
                            }
                        }

                    }
                    if (x < rows - 1) {
                        if (maze[x + 1][y] != getBOMBCODE()) {
                            maze[x + 1][y]++;
                        }
                        if (y > 0) {
                            if (maze[x + 1][y - 1] != getBOMBCODE()) {
                                maze[x + 1][y - 1]++;
                            }
                        }
                        if (y < columns - 1) {
                            if (maze[x + 1][y + 1] != getBOMBCODE()) {
                                maze[x + 1][y + 1]++;
                            }
                        }


                    }
                    if (y > 0) {
                        if (maze[x][y - 1] != getBOMBCODE()) {
                            maze[x][y - 1]++;
                        }
                    }
                    if (y < columns - 1) {
                        if (maze[x][y + 1] != getBOMBCODE()) {
                            maze[x][y + 1]++;
                        }
                    }

                }
                /*
                 * print maze for (int[] a : maze) { System.out.print("\n"); for
                 * (int b : a) { System.out.print(b + "|"); } }
                 * System.out.print("\n"+x+" - "+y+"\n");
                 */
            }
        }
    }

    /**
     * @return the BOMBCODE
     */
    public int getBOMBCODE() {
        return BOMBCODE;
    }

    /**
     * @return the maze
     */
    public int[][] getMaze() {
        return maze;
    }

    /**
     * @return the rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * @return the columns
     */
    public int getColumns() {
        return columns;
    }

    /**
     * @return the bombs
     */
    public int getBombs() {
        return bombs;
    }

    /**
     * @return the totalToBeReaveled
     */
    public int getTotalToBeReaveled() {
        return totalToBeReaveled;
    }
}
